<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;

class Doc extends AdminBase
{
    public function index()
    {
        //查询出所有数据
        $assign['list'] = model('Doc')->order('id DESC')->paginate();
        $assign['page'] = $assign['list']->render();
        $assign['total'] = $assign['list']->total();
        $this->assign($assign);
        return $this->fetch();
    }

    public function add()
    {
        if( $this->request->isPost() )
        {
            $validate = new \app\admin\validate\Doc\Add;
            $param = $this->request->param();
            if (!$validate->check($param))
            {
                return $this->error($validate->getError());
            }
            else
            {
                $param['create_time'] = time();
                $param['is_publish'] = isset($param['is_publish']) ? $param['is_publish'] : 0;
                $id = model('Doc')->insertGetId($param);
                if( $id )
                {
                    //添加一个文档，就往listing表中增加一行数据
                    model('Listing')->insert(['rel_id'=>$id,'category_id'=>$param['category_id'],'is_doc'=>1,'is_publish'=>$param['is_publish']]);
                    return $this->success('操作成功', url('/admin/doc/'));
                }
                else
                {
                    return $this->error(model('Doc')->getError());
                }
            }
        }
        else
        {
            //获取所有一级分类
            $assign['top_categories'] = model('Category')->where("pid", 0)->select()->toArray();
            $this->assign($assign);
            return $this->fetch();
        }
    }

    public function add_new()
    {
        if( $this->request->isPost() )
        {
            $validate = new \app\admin\validate\Doc\Add;
            $param = $this->request->param();
            if (!$validate->check($param))
            {
                return $this->error($validate->getError());
            }
            else
            {
                $param['create_time'] = time();
                $param['is_publish'] = isset($param['is_publish']) ? $param['is_publish'] : 0;
                $id = model('Doc')->insertGetId($param);
                if( $id )
                {
                    //添加一个文档，就往listing表中增加一行数据
                    model('Listing')->insert(['rel_id'=>$id,'category_id'=>$param['category_id'],'is_doc'=>1,'is_publish'=>$param['is_publish']]);
                    return $this->success('操作成功', url('/admin/doc/'));
                }
                else
                {
                    return $this->error(model('Doc')->getError());
                }
            }
        }
        else
        {
            //获取所有一级分类
            $assign['top_categories'] = model('Category')->where("pid", 0)->select()->toArray();
            $this->assign($assign);
            return $this->fetch();
        }
    }

    public function edit()
    {
        $id = $this->request->param('id');
        if( $this->request->isPost() )
        {
            $validate = new \app\admin\validate\Doc\Edit;
            $param = $this->request->param();
            if (!$validate->check($param))
            {
                return $this->error($validate->getError());
            }
            else
            {
                $param['is_publish'] = isset( $param['is_publish'] ) ? $param['is_publish'] : 0;
                $save_status = model('Doc')->save($param, $id);
                if( $save_status !== false )
                {
                    //编辑一个文档，就往listing表中编辑一行数据
                    $where[] = ['rel_id','=',$id];
                    $where[] = ['is_doc','=',1];
                    $save['category_id'] = $param['category_id'];
                    $save['is_publish'] = $param['is_publish'];
                    model('Listing')->save($save,$where);
                    return $this->success('操作成功', url('/admin/doc/'));
                }
                else
                {
                    return $this->error(model('Doc')->getError());
                }
            }
        }
        else
        {
            //获取信息
            $assign['info'] = model('Doc')->find($id);
            //获取所有一级分类
            $assign['top_categories'] = model('Category')->where("pid", 0)->select()->toArray();
            //获取选中的二级分类信息
            $assign['selected_sec_category'] = model('Category')->where([['id','=',$assign['info']['category_id']]])->find();
            //获取选中的一级分类信息
            $assign['selected_top_category'] = model('Category')->where([['id','=',$assign['selected_sec_category']['pid']]])->find();
            //获取所有二级分类
            $assign['sec_categories'] = model('Category')->where([['pid','=',$assign['selected_top_category']['id']]])->select()->toArray();
            $this->assign($assign);
            return $this->fetch();
        }
    }

    public function edit_new()
    {
        $id = $this->request->param('id');
        if( $this->request->isPost() )
        {
            $validate = new \app\admin\validate\Doc\Edit;
            $param = $this->request->param();
            if (!$validate->check($param))
            {
                return $this->error($validate->getError());
            }
            else
            {
                $param['is_publish'] = isset( $param['is_publish'] ) ? $param['is_publish'] : 0;
                $save_status = model('Doc')->save($param, $id);
                if( $save_status !== false )
                {
                    //编辑一个文档，就往listing表中编辑一行数据
                    $where[] = ['rel_id','=',$id];
                    $where[] = ['is_doc','=',1];
                    $save['category_id'] = $param['category_id'];
                    $save['is_publish'] = $param['is_publish'];
                    model('Listing')->save($save,$where);
                    return $this->success('操作成功', url('/admin/doc/'));
                }
                else
                {
                    return $this->error(model('Doc')->getError());
                }
            }
        }
        else
        {
            //获取信息
            $assign['info'] = model('Doc')->find($id);
            //获取所有一级分类
            $assign['top_categories'] = model('Category')->where("pid", 0)->select()->toArray();
            //获取选中的一级分类信息
            $assign['selected_top_category'] = model('Category')->where([['id','=',$assign['info']['category_id']]])->find();
            $this->assign($assign);
            return $this->fetch();
        }
    }

    public function del(){
        $id = $this->request->param('id');
        $validate = new \app\admin\validate\Doc\Del;
        if (!$validate->check(['id'=>$id]))
        {
            return $this->error($validate->getError());
        }
        else
        {
            $res = model('Doc')->where('id', $id)->delete();
            if( $res !== false )
            {
                //删除文档，同时删除listing的关联数据
                model('Listing')->where([['rel_id','=',$id],['is_doc','=',1]])->delete();
                return $this->success('操作成功', url('/admin/doc/'));
            }
            else
            {
                return $this->error(model('Doc')->getError());
            }
        }
    }
}
